package com.cash.app.utils.extra.tools

import android.content.res.Configuration
import android.content.res.Resources
import android.os.Build
import java.util.Locale
import java.util.TimeZone

object LanguageUtils {
    val systemLanguage: Locale
        get() = getLocal(Resources.getSystem().configuration)

    fun getLocal(configuration: Configuration): Locale {
        return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            configuration.locales[0]
        } else {
            configuration.locale
        }
    }

    val currentTimeZone: String
        get() {
            val tz = TimeZone.getDefault()
            //        return TimeZone.getDefault().getDisplayName();
            return createGmtOffsetString(true, true, tz.rawOffset)
        }

    fun createGmtOffsetString(
        includeGmt: Boolean,
        includeMinuteSeparator: Boolean,
        offsetMillis: Int
    ): String {
        var offsetMinutes = offsetMillis / 60000
        var sign = '+'
        if (offsetMinutes < 0) {
            sign = '-'
            offsetMinutes = -offsetMinutes
        }
        val builder = StringBuilder(9)
        if (includeGmt) {
            builder.append("GMT")
        }
        builder.append(sign)
        appendNumber(builder, 2, offsetMinutes / 60)
        if (includeMinuteSeparator) {
            builder.append(':')
        }
        appendNumber(builder, 2, offsetMinutes % 60)
        return builder.toString()
    }

    fun appendNumber(builder: StringBuilder, count: Int, value: Int) {
        val string = value.toString()
        for (i in 0..<count - string.length) {
            builder.append('0')
        }
        builder.append(string)
    }
}
